package cn.jly.flink.source2sink.datastream.source;

import cn.jly.flink.entity.SensorReading;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomUtils;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * @PackageName cn.jly.flink.source2sink.datastream.source
 * @ClassName PrepareData
 * @Description
 * @Author 姬岚洋
 * @Date 2021/1/26 下午 7:36
 */
public class PrepareData {
    public static void main(String[] args) {
        final List<Integer> idList = Arrays.asList(1001, 1002, 1003, 1004, 1005);
        try(BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("d:/" + SensorReading.class.getSimpleName() + ".txt", true))) {
            for (int i = 0; i < 100; i++) {
                final SensorReading sensorReading = new SensorReading();
                sensorReading.setId(idList.get(RandomUtils.nextInt(0, idList.size())).toString());
                sensorReading.setTimestamp(System.currentTimeMillis() + RandomUtils.nextLong(1, 5));
                sensorReading.setTemperature(RandomUtils.nextDouble(10d, 100d));

                // 写入文件
                bufferedWriter.write(JSON.toJSONString(sensorReading));
                bufferedWriter.newLine();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
